//
//  FriendGroup.h
//  Guazi
//
//  Created by koolearn on 11-4-19.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "Statement.h"

#define kGroupAccePublic 1
#define kGroupAccePrivate 2

#define kGroupSQLGroup @"g.id, g.name, g.seq, g.acce, g.is_sync, g.friend_count, g.owner_user_id, g.created_at"


@interface FriendGroup : NSObject {
	
	sqlite_int64 groupId;
    sqlite_int64 userId;
	NSString* name;
	NSInteger seq;
	NSString* createAtString;
	time_t createAt;
	BOOL		isPublic;	//是否公开
    
    BOOL        isSync;     //是否已经与服务器同步
    
	NSInteger friendCount;

	NSMutableArray*  friends;
	
	NSInteger index;
	NSInteger startRowIndex;
	NSInteger endRowIndex;
	
	BOOL		expanded;	//是否被展开了
	BOOL		loaded;		//是否从服务器上加载了
    BOOL        loading;
	BOOL		restored;	//是否从本地数据库中恢复了

}

@property (nonatomic, assign) sqlite_int64 groupId;
@property (nonatomic, assign) sqlite_int64 userId;
@property (nonatomic, retain) NSString* name;
@property (nonatomic, assign) NSInteger seq;
@property (nonatomic, retain) NSString* createdAtString;
@property (nonatomic, assign) time_t createdAt;
@property (nonatomic, assign) BOOL isPublic;
@property (nonatomic, assign) BOOL isSync;
@property (nonatomic, assign) NSInteger friendCount;

@property (nonatomic, retain) NSMutableArray*  friends;

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger startRowIndex;
@property (nonatomic, assign) NSInteger endRowIndex;

@property (nonatomic, assign) BOOL expanded;
@property (nonatomic, assign) BOOL loaded;
@property (nonatomic, assign) BOOL loading;
@property (nonatomic, assign) BOOL restored;


+ (FriendGroup*) groupWithId:(sqlite_int64)agroupId name:(NSString*)aname;
+ (FriendGroup*) groupWithDictionary:(NSDictionary*)dic;
+ (FriendGroup*) groupWithStatement:(Statement*)stmt;
+ (FriendGroup*) groupWithGroup:(FriendGroup*)group;

+ (NSArray*) getGroupsWithUserId:(sqlite_int64)userId;
+ (NSArray*) loadGroups:(BOOL)includeUsers onlyRealGroup:(BOOL)realGroup;
+ (NSArray*) loadGroupsAndUsers;
+ (NSDictionary*) loadContactStatuses;

+ (void) setUsers:(NSArray*)users groupId:(sqlite_int64)groupId;
+ (void) insertGroupUser:(sqlite3_int64)userId groupId:(sqlite3_int64)agroupId seq:(NSInteger)aseq;
+ (void) deleteGroupUser:(sqlite3_int64)userId groupId:(sqlite3_int64)groupId;
+ (void) deleteAllGroupForUser:(sqlite3_int64)userId;
+ (void) deleteAllUserForGroup:(sqlite3_int64)groupId;
+ (void) deleteGroup:(sqlite3_int64)groupId;
- (void) updateDB;
- (NSMutableArray*) restoreUsers;

- (NSDictionary*) dictionary;

@end
